<?php

namespace app\app\controller\v1;
use think\Db;
use app\app\util\OnlineUtils;
use app\app\util\CardUtils;

class RewordController extends BaseController{

	 //集齐卡片兑换金额
	 public function exchangeCard()
	 {
	 	$user_id = OnlineUtils::getUserIdBySid();
	 	//获取用户已有卡id
	 	$cardId  = CardUtils::getUserStarCard($user_id);
	 	if(count($cardId) != 15){
	 		return ft(402,'未集齐15张卡片');
	 	}
	 	//查询是否已经兑换过
	 	$reword = Db::name('reword')->where('user_id','eq',$user_id)->find();
	 	if($reword){
	 		return ft(402,'已经兑换过奖励');
	 	}
	 	//没兑换过
	 	$reword = array('user_id'=>$user_id,'create_time'=>time());
	 	//插入记录
	 	Db::name('reword')->insertGetId($reword);
	 	//卡片数减一
	 	Db::query('update ft_my_card set num =num - 1 where user_id='.$user_id);
	 	//查询出兑换金额
	 	$web_system = Db::name('web_system')->where('id','eq',1)->find();
	 	$fee = $web_system['exchange_reword'];
	 	//用户余额增加
	 	Db::query('update ft_user set fee = fee + '.$fee.' where id ='.$user_id);
	 	//删除卡片id
	 	for($i=1;$i<16;$i++){
	 		CardUtils::sremUserStarCard($user_id,$i);
	 	}
	 	return ft(200,'success');
	}

}